<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>基础测验(编程)</title>
</head>

<body>
  <script>
    // 1.
    /* var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    arr.sort(function (a, b) {
      return 0.5 - Math.random()
    });
    console.log(arr); */

    // 2.
    /* function fn(num) {
      // ?= 正向预查
      // 正则(/(\d)(?=(\d{3})+\.)/ 表示匹配一个数字后面有连续一个或多个的三个数字,并且包含.
      return num && num.toString().replace(/(\d)(?=(\d{3})+\.)/g, function ($1) {
        return $1 + ',';
      });
    }
    console.log(fn(12345678.11)); */

    // 3.
    /* function fn(str) {
      var str1 = '';
      for (var i = str.length - 1; i >= 0; i--) {
        str1 += str[i];
      }
      return str1 == str;
    }
    console.log(fn('qwerewq'));
    console.log(fn('qwerdf')); */

    // 6.
    /* function s(str) {
      return /\d/.test(str);
    }

    function s(str) {
      for (var i = 0; i < str.length; i++) {
        if (str[i] >= 0 || str[i] <= 9) {
          return true;
        }
      }
      return false;
    }
    console.log(s('qwer1234')); //true
    console.log(s('qwerdf')); //false */

    // 8.
    /* function remove(arr, item) {
      var newArr = [];
      newArr = arr.slice(0);
      var index = newArr.indexOf(item);
      while (index !== -1) {
        newArr.splice(index, 1);
        index = newArr.indexOf(item);
      }
      return newArr;
    }
    console.log(remove([1, 2, 3, 4, 2], 2)); */

    // 9.
    /* function fn(num) {
      var str = num.toString(2);
      return str.padStart(8, '0');
    } // ES8中的新语法 */

    /* function fn(num) {
      var str = num.toString(2); // 将num转换为2进制字符串
      var len = 8 - str.length;
      for (var i = 0; i < len; i++) {
        str = '0' + str;
      }
      return str;
    }
    console.log(fn(66)); */

    // 12.
    /* var str = '20150628';
    var year = Math.floor(str / 10000);
    var month = Math.floor(str / 100) % 100;
    var day = str % 100;
    document.write(year + '年' + month + '月' + day + '日'); */

    // 14.
    /* function getRandom(max, min) {
      min = min || 0;
      if (max < min) {
        var max1 = max;
        max = min;
        min = max1;
      }
      var random1 = min + Math.floor(Math.random() * (max - min + 1));
      return random1;
    }
    document.write(getRandom(1, 20)); */

    // 15.
    /* var sum = 0;
    for (var i = 0; i < 64; i++) {
      sum += Math.pow(2, i);
    }
    sum *= 0.00001;
    document.write(sum); */

    // 16.
    /* for (var j = 1; j <= 100; j++) {
      if (j % 7 != 0) {
        var val = j + "";
        if (val.indexOf("7") == -1) {
          document.write(val + '<br>');
        }
      }
    } */

    // 17.
    /* function fn(n) {
      var sum = 1;
      var num = n % 2 == 0 ? 2 : 3;
      for (; num <= n; num += 2) {
        sum += 1 / num;
      }
      return sum;
    }

    // 17-2(递归)
    function fn(n) {
      if (n == 0 || n == 1) {
        return 1;
      }
      return fn(n - 2) + (1 / n);
    }
 */

  </script>
</body>

</html>